const express = require('express');
const pool = require('../utils/pool')
const getId = require('../utils/get-id');
const jwt = require('jsonwebtoken');
const config = require('../config/index');
let router = express.Router();
router.post('/userAdd',(req,res)=>{
  let id = getId();
  let obj = req.body
	pool.query('insert into user set userId = ? , ?',[id,obj],(x,y)=>{
		if(x){
			throw x;
		}
		res.send({code:200,msg:'注册成功'})
	})
 
});
router.get('/userSearch',(req,res)=>{
	var obj= req.query
	pool.query('select*from user where phone = ? and password= ?',[obj.phone,obj.password],(x,y)=>{
		if(x){
			throw x
		}
		if(y.length===0){
			res.send({code:400,msg:'该账号不存在或密码错误'})
		}else{
			res.send({code:200,msg:'登录成功' ,data:y})
		}
		
	})
	
})

router.get('/idSearch',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('select*from user where id = ?',[obj.id],(x,y)=>{
		if(x){
			throw x
		}
		// console.log(y);
		if(y.length===0){
			res.send({code:400,msg:'该账号不存在或密码错误'})
		}else{
			res.send({code:200,msg:'登录成功' ,data:y})
		}
		
	})
	
})
router.get('/adminSearch',(req,res)=>{
	var obj= req.query
	let sql = 'select * from user'
	let params = []
	let conditions = []
	
	// 处理username参数
	if(obj.username && obj.username.trim() !== '') {
		conditions.push('username LIKE ?')
		params.push(`%${obj.username}%`)
	}
	
	// 处理isAdmin参数
	if(obj.isAdmin && obj.isAdmin !== '3') {
		conditions.push('isAdmin = ?')
		params.push(obj.isAdmin)
	}
	
	// 如果有查询条件，添加到SQL语句中
	if(conditions.length > 0) {
		sql += ' where ' + conditions.join(' and ')
	}
	
	pool.query(sql, params, (x,y)=>{
		if(x){
			throw x
		}
		if(y.length===0){
			res.send({code:400,msg:'该账号不存在或密码错误'})
		}else{
			res.send({code:200,msg:'登录成功' ,data:y})
		}
	})
	
})
router.post('/userUpdate',(req,res)=>{
	var obj = req.body
	console.log(obj);
	pool.query('update user set  ? where id = ?',[obj,obj.id],(x,y)=>{
		if(x){
			throw x
		}
		console.log(y);
		if(y.affectedRows===0){
			res.send({code:400,msg:'修改失败'})
		}else{
			res.send({code:200,msg:'修改成功',date:obj})
		}
		
	})
	
})
router.get('/userDelete',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('delete from user where id = ?',[obj.id],(x,y)=>{
		if(x){
			throw x
		}
		console.log(y);
		if(y.affectedRows===0){
			res.send({code:400,msg:'该员工不存在'})
		}else{
			res.send({code:200,msg:'删除成功'})
		}
		
	})
	
})
router.get('/allSearch',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('select*from user ',(x,y)=>{
		if(x){
			throw x
		}
		// console.log(y);
		if(y.length===0){
			res.send({code:400,msg:'该账号不存在或密码错误'})
		}else{
			res.send({code:200,msg:'登录成功' ,data:y})
		}
		
	})
	
})
router.get('/userIdSearch',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('select*from user where userId = ?',[obj.userId],(x,y)=>{
		if(x){
			throw x
		}
		// console.log(y);
		if(y.length===0){
			res.send({code:400,msg:'该账号不存在或密码错误'})
		}else{
			res.send({code:200,msg:'登录成功' ,data:y})
		}
		
	})
	
})

module.exports=router;